#include<vector>
#include<iostream>
#include<cmath>
using namespace std;

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

class Solution {
public:
    vector<int> pathsum;

    void dfs(TreeNode* root, int cur){
        if (!root) return;
        if (!root->left && !root->right) {
            pathsum.push_back(cur);
            return;
        }

        if (root->left) {
            dfs(root->left, cur + root->left->val);
        }
        if (root->right) {
            dfs(root->right, cur + root->right->val);
        }
    }

    bool hasPathSum(TreeNode* root, int targetSum) {
        // 这里是错误的，当root为空时间，无论targetSum为何值，都是应该返回false。
        if (!root) return targetSum==0;
        dfs(root, root->val);
        
        for (auto &x: pathsum){
            if (x == targetSum) return true;
        }
        return false;
    }
};

int main(){
    TreeNode* root = new TreeNode();
    Solution s;
    int targetSum = 0;

    bool res = s.hasPathSum(nullptr, targetSum);
    cout << "hasPathSum: " << (res ? "true" : "false") << endl;

    return 0;
}